package commands;

import games.Mexen.MexenGame;
import games.Mexen.MexenGameImpl;

import java.util.HashMap;
import java.util.List;

import modLoader.ModLoader;

import bot.CommandBot;

public class Mexen implements Command {
	private HashMap<String, MexenGameImpl> mexenGames;

	public Mexen() {
		//ModLoader.newInstance("games.Mexen.Dobbelsteen");
		//ModLoader.newInstance("games.Mexen.Speler");
		
		mexenGames = new HashMap<String, MexenGameImpl>();
	}

	@Override
	public String getCommandName() {
		return "mex";
	}

	@Override
	public boolean execCommand(String channel, String sender, String login,
			String hostname, String message, CommandBot bot) {
		MexenGameImpl mexen = mexenGames.get(channel);

		if (message.startsWith("play ")) {
			newGame(channel, message, bot, mexen);
			return true;
		} else if (mexen != null) {
			if (message.equals("join")) {
				joinGame(channel, sender, bot, mexen);
				return true;
			} else if (message.equals("start")) {
				startGame(channel, bot, mexen);
				return true;
			} else if (sender.equals(mexen.getHuidigeSpelerNaam())) {
				if (message.equals("throw")) {
					playerThrows(channel, bot, mexen);
					return true;
				} else if (message.equals("stop")) {
					playerStops(channel, bot, mexen);
					return true;
				}
				if (mexen.isDone()) {
					mexen = null;
					mexenGames.remove(channel);
				}
			}
		}
		return false;
	}

	private void playerStops(String channel, CommandBot bot,
			MexenGameImpl mexen) {
		List<String> messages = mexen.spelerStopt();
		sendMessages(channel, messages, bot);
	}

	private void playerThrows(String channel, CommandBot bot,
			MexenGameImpl mexen) {
		List<String> messages = mexen.spelerGooit();
		sendMessages(channel, messages, bot);
	}

	private void startGame(String channel, CommandBot bot,
			MexenGameImpl mexen) {
		List<String> messages = mexen.startSpel();
		sendMessages(channel, messages, bot);
	}

	private void joinGame(String channel, String sender, CommandBot bot,
			MexenGameImpl mexen) {
		List<String> messages = mexen.addSpeler(sender);
		sendMessages(channel, messages, bot);
	}

	private void sendMessages(String channel, List<String> messages, CommandBot bot) {
		for (String m : messages) {
			bot.sendMessage(channel, m, true);
		}
	}

	private void newGame(String channel, String message, CommandBot bot,
			MexenGameImpl mexen) {
		try {
			if (mexen != null) {
				bot.sendMessage(channel, "A game is currently running", true);
				return;
			}
			int maxThrows = Integer.parseInt(message.substring(5));
			mexen = (MexenGameImpl) ModLoader.newInstance("games.Mexen.MexenGame");
			mexen.setAantalWorpen(maxThrows);
			mexenGames.put(channel, mexen);
			if (maxThrows >= 1 && maxThrows <= 10) {
				bot.sendMessage(channel, "New game made, type " + CommandBot.prefix
						+ "mexjoin to join the game and "+CommandBot.prefix +"to start the game", true);
			} else {
				bot.sendMessage(channel, "Give a number between 1 and 10", true);
			}
		} catch (NumberFormatException e) {
			bot.sendMessage(channel, "Input not a number", true);
		} catch (IndexOutOfBoundsException e) {
			bot.sendMessage(channel,
					"Give a number for max throws between 1 and 10", true);
		}
	}

	@Override
	public boolean execCommand(String sender, String login, String hostname,
			String message, CommandBot bot) {
		if (message.startsWith(" ")) {
			bot.sendMessage(sender, "Multiplayer only", true);
			return true;
		}
		return false;

	}
}
